package com.qiyu.tech.search.controller;

import com.alibaba.fastjson.JSON;
import com.qiyu.core.cache.redis.client.RedisService;
import com.qiyu.tech.search.dao.ExperienceInfoDao;
import com.qiyu.tech.search.dao.UserInfoDao;
import com.qiyu.tech.search.dao.VipInfoDao;
import com.qiyu.tech.search.model.ExperienceInfo;
import com.qiyu.tech.search.model.UserInfo;
import com.qiyu.tech.search.model.VipInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.Tuple;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author linhao
 * @Date created in 5:34 下午 2020/12/21
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserInfoDao userInfoDao;

    @Resource
    private VipInfoDao vipInfoDao;

    @Resource
    private ExperienceInfoDao experienceInfoDao;

    @Resource
    private RedisService redisService;

    @GetMapping(value = "batchInsert")
    public Boolean batchInsert(){
        HashMap<String,String> multiMap = new HashMap<>(1000);
        for(int i=0;i<1000;i++){
            multiMap.put("user-info-test-"+i,i+"");
//            redisService.setStr("key-test-"+i,i+"", 60, TimeUnit.DAYS);
        }
        redisService.multiSet(multiMap,900);
        return true;
    }

    @GetMapping(value = "batchInsertSet")
    public Boolean batchInsertSet(){
        HashMap<String,String> multiMap = new HashMap<>(1000);
        for(int i=0;i<1000;i++){
            multiMap.put("user-info-test-"+i,i+"");
        }
        redisService.setMapEx("user-love-map",multiMap,60 * 60 * 24);
        return true;
    }

    @GetMapping(value = "/query")
    public Set<String> query() {
        if (!redisService.exists("vip-info-set")) {
            List<VipInfo> vipInfos = vipInfoDao.selectAll();
            List<String> jsonVipInfoList = new ArrayList<>(vipInfos.size());
            for (VipInfo vipInfo : vipInfos) {
                jsonVipInfoList.add(String.valueOf(vipInfo.getUserId()));
            }
            redisService.sAdd("vip-info-set", Arrays.asList(jsonVipInfoList).toString());
        }

        if (!redisService.exists("exp-info-set")) {
            List<ExperienceInfo> experienceInfos = experienceInfoDao.selectAll();
            List<String> experienceInfoList = new ArrayList<>(experienceInfos.size());
            for (ExperienceInfo experienceInfo : experienceInfos) {
                experienceInfoList.add(String.valueOf(experienceInfo.getUserId()));
            }
            redisService.sAdd("exp-info-set", Arrays.asList(experienceInfoList).toString());
        }

        if (!redisService.exists("user-info-set")) {
            List<UserInfo> userInfos = userInfoDao.selectFromUser01();
            List<String> userInfoList = new ArrayList<>(userInfos.size());
            for (UserInfo userInfo : userInfos) {
                userInfoList.add(String.valueOf(userInfo.getMemberId()));
            }
            redisService.sAdd("user-info-set", Arrays.asList(userInfoList).toString());
        }
        Set<String> resultSet = redisService.sInter("vip-info-set", "exp-info-set", "user-info-set");
        return resultSet;
    }

    @GetMapping(value = "doTest4")
    public List<String> doTest4(String cursor,String pattern,Integer count){
        ScanParams scanParams = new ScanParams();
        scanParams.match(pattern);
        scanParams.count(count);
        return redisService.scan(cursor,scanParams);
    }


    @GetMapping(value = "scanAll")
    public Set<String> scanAll(String cursor,String pattern,Integer count){
        return redisService.scanAll(cursor,pattern,count);
    }


    @GetMapping(value = "sScanAll")
    public List<String> sScanAll(String key,String cursor,String pattern,Integer count){
        return redisService.sScanAll(key,cursor,pattern,count);
    }


    @GetMapping(value = "hScanAll")
    public Map<String,String>  hScanAll(String key,String cursor,String pattern,Integer count){
        return redisService.hScanAll(key,cursor,pattern,count);
    }


    @GetMapping(value = "zScanAll")
    public List<Tuple> zScanAll(String key, String cursor, String pattern, Integer count){
        return redisService.zScanAll(key,cursor,pattern,count);
    }
}
